<script>
	$(function(){
		$('a.process').each(function(i,v){
			$(v).attr('code', $(v).attr('href'));
			$(v).attr('href', 'javascript:void(0);');
		});
		// 将按钮移到title上
		$ul = $('<ui/>').addClass('block-options');
		$('.data-table-toolbar:eq(0) .toolbar-btn-action a').each(function(index, element){
			$ul.append('<li>'+this.outerHTML+'</li>');
		});
		$('h3.block-title:contains("队列任务")').prev().after($ul);
		$('.tab-pane .data-table-toolbar:eq(0)').remove();
	});
	$.loadQueue = function (code, doScript, doAjax) {
		var box = layer.open({
			type: 1, title: false, area: ['auto', '315px'], anim: 2, shadeClose: false, end: function () {
				doAjax = false;
			}, content: '' +
				'<div class="padding-30 padding-bottom-0" style="width:500px" data-queue-load="' + code + '">' +
				'   <div class="layui-elip nowrap" data-message-title></div>' +
				'   <div class="margin-top-15 layui-progress layui-progress-big" lay-showPercent="yes"><div class="layui-progress-bar transition" lay-percent="0.00%"></div></div>' +
				'   <div class="margin-top-15"><textarea class="layui-textarea layui-bg-black border-0" disabled style="resize:none;overflow:hidden;height:190px"></textarea></div>' +
				'</div>'
		});
		(function loadprocess(code, that) {
			that = this, this.$box = $('[data-queue-load=' + code + ']');
			if (doAjax === false || that.$box.length < 1) return false;
			this.$area = that.$box.find('textarea'), this.$title = that.$box.find('[data-message-title]');
			this.$percent = that.$box.find('.layui-progress div'), this.runCache = function (code, index, value) {
				this.ckey = code + '_' + index, this.ctype = 'admin-queue-script';
				return value !== undefined ? layui.data(this.ctype, {key: this.ckey, value: value}) : layui.data(this.ctype)[this.ckey] || 0;
			};
			this.setState = function (status, message) {
				if (message.indexOf('javascript:') === -1) if (status === 1) {
					that.$title.html('<b class="color-text">' + message + '</b>').addClass('text-center');
					that.$percent.addClass('layui-bg-blue').removeClass('layui-bg-green layui-bg-red');
				} else if (status === 2) {
					if (message.indexOf('>>>') > -1) {
						that.$title.html('<b class="color-blue">' + message + '</b>').addClass('text-center');
					} else {
						that.$title.html('<b class="color-blue">正在处理：</b>' + message).removeClass('text-center');
					}
					that.$percent.addClass('layui-bg-blue').removeClass('layui-bg-green layui-bg-red');
				} else if (status === 3) {
					that.$title.html('<b class="color-green">' + message + '</b>').addClass('text-center');
					that.$percent.addClass('layui-bg-green').removeClass('layui-bg-blue layui-bg-red');
				} else if (status === 4) {
					that.$title.html('<b class="color-red">' + message + '</b>').addClass('text-center');
					that.$percent.addClass('layui-bg-red').removeClass('layui-bg-blue layui-bg-green');
				}
			};
			$.post('progress', {code: code}, function (ret) {
				if (ret.code) {
					that.lines = [];
					for (this.lineIndex in ret.data.history) {
						this.line = ret.data.history[this.lineIndex], this.percent = '[ ' + this.line.progress + '% ] ';
						if (this.line.message.indexOf('javascript:') === -1) {
							that.lines.push(this.line.message.indexOf('>>>') > -1 ? this.line.message : this.percent + this.line.message);
						} else if (!that.runCache(code, this.lineIndex) && doScript !== false) {
							that.runCache(code, this.lineIndex, 1), location.href = this.line.message;
						}
					}
					that.$area.val(that.lines.join("\n")), that.$area.animate({scrollTop: that.$area[0].scrollHeight + 'px'}, 200);
					that.$percent.attr('lay-percent', (parseFloat(ret.data.progress || '0.00').toFixed(2)) + '%'), layui.element.render();
					if (ret.data.status > 0) that.setState(parseInt(ret.data.status), ret.data.message);
					else return that.setState(4, '获取任务详情失败！'), false;
					if (parseInt(ret.data.status) === 3 || parseInt(ret.data.status) === 4) return false;
					return setTimeout(function () {
						loadprocess(code);
					}, Math.floor(Math.random() * 200)), false;
				}else{
					layer.close(box);
					Dolphin.notify(ret.msg, 'danger');
				}
			}, 'json');
		})(code)
	};
</script>